# 用户管理
from fastapi import APIRouter
from sqlalchemy.orm import Session

from models import User, engine
import string
import secrets
router = APIRouter()

def generate_api_key(length: int = 32) -> str:
    base62_chars = string.digits + string.ascii_letters
    random_string = ''.join(secrets.choice(base62_chars) for _ in range(length))
    return "sk-" + random_string
@router.get("/user")
def get_user():
    with Session(engine) as session:
        user = session.get(User, "c05d3d7f-28f8-4277-88cd-bea5ace34c7f")
        if user:
            return {"status": 2, "user_id": user.id}

        test = User(
            id="c05d3d7f-28f8-4277-88cd-bea5ace34c7f",
            name="测试",
            password="<PASSWORD>",
            api_key=generate_api_key(),
        )
        session.add(test)
        session.commit()
        session.refresh(test)
    return {"status": 1, "user_id": test.id}
